package com.ukao.cashregister.pesenter;

import android.content.Context;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.AllMarkprintBean;
import com.ukao.cashregister.bean.BaseBean;
import com.ukao.cashregister.bean.MarkprintBean;
import com.ukao.cashregister.bean.MercCityBean;
import com.ukao.cashregister.bean.PayInfoBean;
import com.ukao.cashregister.bean.ProductRelImgBean;
import com.ukao.cashregister.bean.ReceiptBean;
import com.ukao.cashregister.bean.StockinBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.StockinView;

import java.util.ArrayList;
import java.util.HashMap;

import static android.R.attr.id;
import static com.ukao.cashregister.R.id.transNo;

/**
 * Created by ukaodev on 2017/6/26.
 */

public class StockinPesenter extends BasePresenter<StockinView> {



    public StockinPesenter(StockinView view) {
        attachView(view);
    }




    /**
     *打印订单
     */
    public void printReceipt(Context context, String  id ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.printReceipt(Constant.createParameter(mHashMap)),
                new ApiCallback<ReceiptBean>() {
                    @Override
                    public void onSuccess(ReceiptBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadReceipt(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *搜索订单
     */
    public void searchStorage(Context  context,String   orderNo ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("keywords",orderNo);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.searchStorage(Constant.createParameter(mHashMap)),
                new ApiCallback<StockinBean>() {
                    @Override
                    public void onSuccess(StockinBean model) {

                        if (model.getHttpCode()==200){
                                mvpView.getDataSuccess(model.getData());
                        }else{

                             mvpView.loadFail(model.getMsg());
                        }


                   }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *删除衣物
     */
    public void deleteStockIn(Context  context,String    id,final    StockinBean.ProListBean item ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.deleteStockIn(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.deleteStockInSuccess("删除成功",item);
                        }else{

                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     *入库快递信息
     */
    public void stockInMercCity(Context  context ) {

        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.mercCity(Constant.createParameter(mHashMap)),
                new ApiCallback<MercCityBean>() {
                    @Override
                    public void onSuccess(MercCityBean model) {
                        if (model.getHttpCode()==200){
                           mvpView.logisticsSuccess(model);
                        }else{

                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {

                    }

                });
    }


    /**
     *复制
     */
    public void copystockIn( Context  mContext,String orderId,    StockinBean.ProListBean mProListBean,int sortNo  ) {

        HashMap mHashMap=new HashMap();

         String      businessId=mProListBean.getBusinessId()+"";  /*项目的businessId*/
        String    businessName=mProListBean.getBusinessName();/*项目名称*/
        String  projectId=mProListBean.getMercBusinessId();/*项目id*/
        String    currentParentId=mProListBean.getProductId()+"";/*衣物详情列表id*/
        String     proTypeId=mProListBean.getProTypeId()+"";/*衣物详情列表tab*/
        String     count=CheckUtils.isNull(mProListBean.getCount())? "" :mProListBean.getCount()+"";/*服务数量*/
        String      serviceName=mProListBean.getServiceName();/*服务名称*/
        String     proServiceId=mProListBean.getProServiceId()+"";/*服务id*/
        String    brandId= CheckUtils.isEmptyString(mProListBean.getBrandId());/*品牌id*/
        String    brandName=  CheckUtils.isEmptyString(mProListBean.getBrandName());/*品牌*/
        String    chgSubtotal=CheckUtils.isNull(mProListBean.getChgSubtotal())? "" :mProListBean.getChgSubtotal()+"";/*修改金额*/

        if (!CheckUtils.isEmpty(mProListBean.getColorDesc())){
           String strcolorDesc=mProListBean.getColorDesc();
            mHashMap.put("colorDesc", strcolorDesc);  /*颜色*/
        }
        if(!CheckUtils.isEmpty(mProListBean.getAnnexDesc())){
            String annexDesc=mProListBean.getAnnexDesc();
         mHashMap.put("annexDesc", annexDesc);/*附件*/}
        if(!CheckUtils.isEmpty(mProListBean.getFlawDesc())) {
            String flawDesc=mProListBean.getFlawDesc();
             mHashMap.put("flawDesc", flawDesc);/*瑕疵*/
        }
        if(!CheckUtils.isEmpty(mProListBean.getEffectDesc())){
            String effectDesc= mProListBean.getEffectDesc();
          mHashMap.put("effectDesc", effectDesc);/*洗后效果*/}
        if(!CheckUtils.isEmpty(mProListBean.getAddServiceDesc())){
            String  addServiceDesc= mProListBean.getAddServiceDesc();
            mHashMap.put("addServiceDesc", addServiceDesc);/*附加服务*/
        }

        mHashMap.put("orderId",orderId);
        mHashMap.put("businessId",businessId);/*项目的businessId*/
        mHashMap.put("mercBusinessId",projectId);/*项目id*/
        mHashMap.put("businessName",businessName);/*项目名称*/
        mHashMap.put("productId",currentParentId);/*衣物详情列表id*/
        mHashMap.put("proTypeId",proTypeId);/*衣物详情列表tab*/
        mHashMap.put("count", count);/*服务数量*/
        mHashMap.put("serviceName", serviceName);/*服务名称*/
        mHashMap.put("proServiceId", proServiceId);/*服务id*/
        mHashMap.put("brandId",brandId.equals("null")&&CheckUtils.isNull(brandId)?"": brandId);/*品牌id*/
        mHashMap.put("brandName", brandName);/*品牌*/
        mHashMap.put("chgSubtotal", chgSubtotal);/*修改金额*/
        mHashMap.put("sortNo", sortNo+"");
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        mvpView.showLoading();
        addSubscription(apiStores.finishstockIn(Constant.createParameter(mHashMap)),
                new ApiCallback<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode() == 200) {

                            mvpView.copySuccess("复制成功");

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }




                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }



    /**
     *修改总金额
     */
    public void modifyPrice(Context  context,String  orderid,String modifyPrice,final String  strmodifyPrice ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("id",orderid);
        mHashMap.put("modifyPrice",modifyPrice);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.modifyPrice(Constant.createParameter(mHashMap)),
                new ApiCallback<BaseBean>() {
                    @Override
                    public void onSuccess(BaseBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.modifySuccess(strmodifyPrice);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                mvpView.hideLoading();
                    }

                });
    }

    /**
     *完成入库
     */
    public void finishStorage(Context  context,String  orderid  ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("id",orderid);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.finishStorage(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.finishStorageSucceed(model.getMsg());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }




    /**
     *打标
     */
    public void printWash(Context  context,final  String  orderProId   ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderProId",orderProId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.printWash(Constant.createParameter(mHashMap)),
                new ApiCallback<MarkprintBean>() {
                    @Override
                    public void onSuccess(MarkprintBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.marKprintSucceed(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     *全打标
     */
    public void allprintWash(Context  context,final  String  orderId) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.allprintWash(Constant.createParameter(mHashMap)),
                new ApiCallback<AllMarkprintBean>() {
                    @Override
                    public void onSuccess(AllMarkprintBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.allmarKprintSucceed(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *检测订单是否支付
     */

    public void queryPayStatus( String  id) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.queryPayStatus(Constant.createParameter(mHashMap)),
                new ApiCallback<PayInfoBean>() {
                    @Override
                    public void onSuccess(PayInfoBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadPayStatus(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 查询衣物图片列表
     * @param id
     */
    public void productImagelist( final String  id) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderProId",id);/*订单衣物id*/
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.productlist(Constant.createParameter(mHashMap)),
                new ApiCallback<ProductRelImgBean>() {
                    @Override
                    public void onSuccess(ProductRelImgBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.photoListSucceed((ArrayList<ProductRelImgBean>) model.getData(),  id);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *

     修改工厂运单号
     * @param orderId
     * @param transNo  工厂运单号
     */
    public void updateFactoryTransNo(  String  orderId,final String  transNo ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);/*订单衣物id*/
        mHashMap.put("transNo",transNo);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.updateFactoryTransNo(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.updateFactorySucceed(transNo);
                        }else{
                           mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *
     * @param
     * @param bindCode  标号
     */
    public void bindCode(StockinBean.ProListBean item,final String  bindCode ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",item.getId());
        mHashMap.put("bindCode",bindCode);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.bindCode(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            item.setBindCode(bindCode);
                            mvpView.bindCodeSucceed(item);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }
                    }
                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }

    /**
     * 订单衣物洗涤中
     * @param
     * @param
     */
    public void proceedWashing(String orderId) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.proceedWashing(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                         mvpView.washingSucceed();
                        }else{
                    mvpView.loadFail(model.getMsg());
                        }
                    }
                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }
}
